# coding=utf-8
# 全世界最帅的帅哥
from werkzeug.security import generate_password_hash, check_password_hash
from ysgg1_club_comment.modules import db
from ysgg1_club_comment.modules.base_model import BaseModel
from sqlalchemy.dialects.mssql import TINYINT


class AuthUser(BaseModel):
    __tablename__ = 'auth_user'

    user_name = db.Column(db.String(32), comment="用户名称/账号")
    nick_name = db.Column(db.String(32), comment="昵称")
    email = db.Column(db.String(32), comment="邮箱")
    phone = db.Column(db.String(32), comment="手机号")
    password = db.Column(db.String(256), comment="密码")
    sex = db.Column(TINYINT, comment="性别 0男性 1女性", default=0)
    avatar = db.Column(db.String(255), comment="头像")
    status = db.Column(TINYINT, comment="状态 0启动, 1禁用", default=0)
    introduce = db.Column(db.String(255), comment="个人介绍")
    extra_json = db.Column(db.String(255), comment="特殊字段")

    @property
    def pwd(self):
        return self.password  # 加密之后的密文

    # 定义pwd的setter函数
    @pwd.setter
    def pwd(self, x_password):
        """
        根据明文的密码，加密得到密文
        :param x_password: 明文密码
        :return:
        """
        # 根据flask提供的算法来加密
        self.password = generate_password_hash(x_password)

    def check_password(self, x_password):
        """
        根据明文的密码，加密得到密文
        :param x_password: 明文密码
        :return:
        """

        return check_password_hash(self.password, x_password)

    def __repr__(self):
        return f'<AuthUser {self.id}>'


